package com.raycloud.java.dada.service.impl;

import com.alibaba.fastjson.JSON;
import com.raycloud.java.dada.base.BaseService;
import com.raycloud.java.dada.base.Config;
import com.raycloud.java.dada.model.*;
import com.raycloud.java.dada.result.OrderAddResult;
import com.raycloud.java.dada.result.OrderCancelResult;
import com.raycloud.java.dada.result.OrderDeliverResult;
import com.raycloud.java.dada.result.ResultModel;
import com.raycloud.java.dada.service.inter.OrderService;
import com.raycloud.java.dada.util.BasicURL;

/**
 * Created by:  intelliJ IDEA
 *
 * @Author albert
 * @Description:订单操作接口实现
 * @Date:2017/6/23 下午3:10
 * @package:com.raycloud.java.dada.service.impl
 * @Modified By:
 */
public class OrderServiceImpl extends BaseService implements OrderService {

    public OrderServiceImpl(Config config) {
        super(config);
    }


    @Override
    public OrderAddResult addNewOrder(NewOrder order) throws Exception {
        String result = doPost(order, BasicURL.ORDER_ADD);
        ResultModel model = strToResultModel(result);
        OrderAddResult orderAddResult = null;
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            orderAddResult = JSON.parseObject(model.getResult(), OrderAddResult.class);
        }

        return orderAddResult;
    }

    @Override
    public OrderAddResult reAddOrder(ReAddOrder reAddOrder) throws Exception {
        String result = doPost(reAddOrder, BasicURL.ORDER_READD);
        ResultModel model = strToResultModel(result);
        OrderAddResult orderAddResult = null;
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            orderAddResult = JSON.parseObject(model.getResult(), OrderAddResult.class);
        }

        return orderAddResult;
    }

    @Override
    public OrderDeliverResult queryDeliverFee(PreAddOrder preAddOrder) throws Exception {
        String result = doPost(preAddOrder, BasicURL.ORDER_DELIVERY_FEE);
        ResultModel model = strToResultModel(result);
        OrderDeliverResult orderDeliverResult = null;
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            orderDeliverResult = JSON.parseObject(model.getResult(), OrderDeliverResult.class);
        }

        return orderDeliverResult;
    }


    @Override
    public ResultModel deliveryAddOrder(OrderDeliver orderDeliver) throws Exception {
        String result = doPost(orderDeliver, BasicURL.ORDER_DELIVERY_ADD);
        ResultModel model = strToResultModel(result);

        return model;
    }


    @Override
    public ResultModel addTip(OrderTips orderTips) throws Exception {
        String result = doPost(orderTips, BasicURL.ORDER_ADD_TIP);
        ResultModel model = strToResultModel(result);

        return model;
    }

    @Override
    public OrderCancelResult cancelOrder(OrderCancel orderCancel) throws Exception {
        String result = doPost(orderCancel, BasicURL.ORDER_FORMAL_CANCEL);
        ResultModel model = strToResultModel(result);
        OrderCancelResult orderCancelResult = null;
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            orderCancelResult = JSON.parseObject(model.getResult(), OrderCancelResult.class);
        }

        return orderCancelResult;
    }

    @Override
    public ResultModel appointOrder(AppendOrder appendOrder) throws Exception {
        String result = doPost(appendOrder, BasicURL.ORDER_APPOINT);
        ResultModel model = strToResultModel(result);

        return model;
    }

    @Override
    public ResultModel cancelAppointOrder(CancelAppendOrder cancelAppendOrder) throws Exception {
        String result = doPost(cancelAppendOrder, BasicURL.ORDER_APPOINT_CANCEL);
        ResultModel model = strToResultModel(result);

        return model;
    }
}
